// pages/user-setup-information/index.js
const handler=require("../../utils/function.js");
const { $Message } = require('../../dist/base/index');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userData:[],
    avatar:"http://xyds.4hl.cn/xyds/uploads/otherimg/index/teacher-avatar.png",
    nickname:"lucky",
    gender_list: [
      { name: '1', value: '男', checked: 'true' },
      { name: '0', value: '女' },
    ],
    gender:0,
    sign:"请输入签名。。。",
    collage:"西安大学",
    phone:"13212341234",
    address:"陕西省显示未央区海洋大厦704号"
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData();
  },
  upload_avatar:function(res){
    const that=this;
    handler.uploadImageHandler((res)=>{
      wx.uploadFile({
        url: 'http://xyds.4hl.cn/xyds/index.php/uploadImg',
        filePath: res[0],
        name: 'image',
        success:function(e){
          console.log(e.data)
          that.setData({ avatar: e.data });
        },
        fail:function(e){
          wx.showToast({
            title: "上传失败",
            icon: 'loading'
          })
        }
      })
      // 
    })
  },
  initData: function () {
    var that = this;
    var para = {
      "user_id": wx.getStorageSync('user_info').id,
      "user_type": wx.getStorageSync('user_info').user_type
    };
    handler.requestData("getUserInfo", para, function (res) {
      if (res.success == true) {
        var temp = that.data.gender_list;
        for(var i =0; i < temp.length; i++){
      
          if(temp[i]['name'] == res.data.sex){
            temp[i]['checked'] = true;
          }else{
            temp[i]['checked'] = false;
          }
        }
        that.setData({
          userData: res.data,
          gender_list:temp,
          avatar:res.data.avatar,
          nickname:res.data.nick_name,
          gender:res.data.sex,
          sign: res.data.signature,
          collage: res.data.school,
          address:res.data.address,
          phone:res.data.phone
        })
        wx.hideNavigationBarLoading(); //完成停止加载
        wx.stopPullDownRefresh(); //停止下拉刷新
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading'
        })
      }
    });
  },

  // 获取昵称
  get_nickname:function(e){
    var test_nickname = handler.regTest(e.detail.value, "请输入姓名");
    test_nickname && this.setData({ nickname: e.detail.value });
  },
  //获取性别
  radioChange: function (e) {
    // console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({ gender: e.detail.value});
  },
  // 获取电话
  get_phone:function(e){
    var test_phone = handler.regTest(e.detail.value, "请输入正确的电话号码格式", /^1\d{10}$/);
    test_phone && this.setData({ phone: e.detail.value });
  },
  // 获取地址
  get_address: function (e) {
    var test_address = handler.regTest(e.detail.value, "请输入地址");
    test_address && this.setData({ address: e.detail.value });
  },
  // 获取个性签名
  get_sign:function(e){
  
    var test_sign = handler.regTest(e.detail.value, "请输入签名");
    test_sign && this.setData({ sign: e.detail.value });
  },
  // 获取学校
  get_collage: function (e) {
    var test_collage = handler.regTest(e.detail.value, "请输入地址");
    test_collage && this.setData({ collage: e.detail.value });
  },
  // 获取信息
  get_information:function(){
    const { avatar, nickname, gender,sign,collage,address,phone}=this.data;
    var nickname_test = handler.regTest(nickname, "请输入昵称")
    var phone_test = handler.regTest(phone, "请输入电话号码", /^1\d{10}$/);
    var address_test = handler.regTest(address, "请输入地址");
    var sign_test = handler.regTest(sign, "请输入签名");
    var collage_test = handler.regTest(collage, "请输入学校");
    var test_all = nickname_test && phone_test && address_test && sign_test && collage_test;
    
    var submitDataJson = {
      avatar: avatar,
      nick_name:nickname,
      sex:gender,
      signature:this.data.sign,
      school:collage,
      address:address,
      phone:phone,
      id:wx.getStorageSync('user_info').id
    };
    if (test_all){
      var that = this;
      handler.requestData("modeifyUserInfo", submitDataJson, function (res) {
        if (res.success == true) {
           wx.setStorageSync('user_info', res.data);
           $Message({
            content: '修改成功',
            type: 'success'
           });
          handler.intoPageHandler("../personal-information/index");
        } else {
          wx.showToast({
            title: res.message,
            icon: 'loading'
          })
        }
      });

    }else{
      $Message({
        content: '信息输入有误，请重新输入',
        type: 'error'
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //下拉刷新
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.initData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})